/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entities.Account;
import java.io.IOException;
import java.math.BigDecimal;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author QUY_THI
 */
public class EditAccountBean {

    @EJB
    private EditAccountLocal editAccount;
    @EJB
    private AccountDetailLocal accountDetail;
    private String accountCode;
    private String accountName;
    private String address;
    private String indent;

    public String getAccountName() {
        return accountDetail.accountDetail(accountCode).getAccountName();
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getAddress() {
        return accountDetail.accountDetail(accountCode).getAddress();
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getIndent() {
        return accountDetail.accountDetail(accountCode).getIdentificationCard();
    }

    public void setIndent(String indent) {
        this.indent = indent;
    }

    public String getAccountCode() {
        return accountCode;
    }

    public void setAccountCode(String accountCode) {
        this.accountCode = accountCode;
    }

    /** Creates a new instance of EditAccountBean */
    public EditAccountBean() {
    }

    public String edit() {
        Account acc = accountDetail.accountDetail(accountCode);
        acc.setAcountCode(accountCode);
        acc.setAddress(address);
        acc.setAccountName(accountName);
        acc.setIdentificationCard(indent);
        boolean editAccount1 = editAccount.editAccount(acc);
        if (editAccount1) {
            FacesMessage msg = new FacesMessage("success", "Edit Successfull !! ");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return "home.xhtml?faces-redirect=true";
        }
        FacesMessage msg = new FacesMessage("error", "Edit false !! ");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return "";
    }

    public String getAutoRedirect() throws IOException {
        if (accountCode == null || accountCode.equals("")) {
            FacesContext context = FacesContext.getCurrentInstance();
            ((HttpServletResponse) context.getExternalContext().getResponse()).sendRedirect("../faces/home.xhtml");
        }
        return "";
    }
}
